/* Goldfinger -- Experimental Scala compiler backend targetting AVM2 (Tamarin)  
 * Copyright 2008 Szymon Jachim
 * @author  Szymon Jachim
 */

// $Id: GenABC.scala  $

package com.googlecode.goldfinger

import java.io.{DataOutputStream, File, OutputStream}
import java.nio.ByteBuffer

import scala.tools.nsc 
import nsc.Global
import nsc.Phase 
import nsc.plugins.Plugin
import nsc.plugins.PluginComponent

import scala.collection.immutable.{Set, ListSet}
import scala.collection.mutable.{Map, HashMap, HashSet}
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.symtab._ 
import scala.tools.nsc.util.{Position, NoPosition}

class GenABCPlugin(val global: Global) extends Plugin {
  import global._
  import icodes._
  import icodes.opcodes._  
  
  val name = "abc"
  val description = "generates avm2 bytecode"
  val components = List[PluginComponent](GenABC)
  
  private object GenABC extends PluginComponent {
    val global = GenABCPlugin.this.global
    val runsAfter = "icode"
    val phaseName = GenABCPlugin.this.name
    def newPhase(prev: Phase) = new AbcPhase(prev)    
  }
  
  private class AbcPhase(prev: Phase) extends Phase(prev) {
    override def erasedTypes = true
    def name = GenABCPlugin.this.name
    object codeGenerator extends AbcGenerator
    def run {
      if (settings.debug.value) inform("[running phase " + name + " on icode]")

      classes.values foreach codeGenerator.genClass
    }

    def apply(unit: CompilationUnit) {
      abort("ABC works on icode classes, not on compilation units!")
    }
  } 
  
  class AbcGenerator {
    def genClass(c: IClass) {
      object abc extends ActionBlock(new macromedia.asc.embedding.avmplus.ByteCodeFactory) {
        
      }
      val bytes = abc.emit
      // bytes. TODO jakos te bajty zpaisac do pliku
      inform("generated avm2 bytecode for " + c.symbol.name)
    }
  }
}  
